﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Custom;
using PlugNT.Common;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Comment
{
    public partial class Admin : AdminPage
    {
        private Channel curChannel = new Channel();
        private Comment curComment = new Comment();

        public string keyword = "";
        public string isexact = "";
        public string islocked = "";
        public string showsql = "";

        public string currpostfix = "";
        private int pageSize = 15;
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            currpostfix = (Request.QueryString["currpostfix"] == null) ? WebConfig.TablePostfix : Request.QueryString["currpostfix"].Trim();
            curComment.CurPostfix = currpostfix;
            if (!IsPostBack)
            {

                try
                {
                    DdlMenu_bind();

                    keyword = (Request.QueryString["keyword"] == null) ? "" : Request.QueryString["keyword"].Trim();
                    isexact = (Request.QueryString["isexact"] == null) ? "0" : Request.QueryString["isexact"].Trim();
                    islocked = (Request.QueryString["islocked"] == null) ? "0" : Request.QueryString["islocked"].Trim();
                    showsql = (Request.QueryString["showsql"] == null) ? "0" : Request.QueryString["showsql"].Trim();

                    switch (Request["t"])
                    {
                        case "delete":
                            Delete_msgs(Convert.ToInt32(Request["id"]));
                            break;
                        case "updatetip":
                            UpdateTip(Convert.ToInt32(Request["id"]), Request["value"]);
                            break;
                    }

                    Repeater_admin_bind(keyword);
                }
                catch (Exception ex) { Response.Redirect("/Admin/Message.aspx?message=数据绑定错误！" + ex.Message + "&z=no"); }
            }
        }
        private void Repeater_admin_bind(string keyword)
        {
            int page = 0;
            if (Request["page"] != null)
            {
                page = Convert.ToInt32(Request["page"]);
            }

            bool tmp_isShowSql = showsql == "1" ? true : false;  //是否显示sql（调试用）
            bool tmp_isExact  = isexact == "1" ? true : false;
            bool tmp_isLocked  = islocked == "1" ? true : false;

            int recordCount = 0;   //Comment.GetCount(where);
            DataTable dt = curComment.GetList(pageSize, (page - 1), keyword, tmp_isExact, tmp_isLocked, tmp_isShowSql, out recordCount);

            litPager.Text = Pager.Pagination(recordCount, pageSize, page, "Admin.aspx", GetBindUrl(), "", true, true);

            Repeater_admin.DataSource = dt.DefaultView;
            Repeater_admin.DataBind();

        }
        private void DdlMenu_bind()
        {
            DataTable dt = new Tables().GetList();
            this.ddlTables.Items.Clear();
            //this.ddlTables.Items.Add(new ListItem("默认表", "")); 
            foreach (DataRow dr in dt.Rows)
            {
                string description = dr["description"].ToString().Trim();
                string postfix = dr["postfix"].ToString().Trim();
                bool crecomment = (bool)dr["crecomment"];
                if (crecomment)
                {
                    ListItem li = new ListItem(description, postfix);
                    this.ddlTables.Items.Add(li);
                }
            }
            this.ddlTables.SelectedValue = currpostfix.ToString();
            this.ddlTables.Attributes.Add("onchange", "SubmitUrl();");
        }
        private void UpdateTip(int id, string tValue)
        {
            CommentInfo model = new CommentInfo();
            model.id = id;
            model.tip = int.Parse(tValue);
            curComment.UpdateTip(model);
        }

        //删除一条数据
        void Delete_msgs(int id)
        {
            curComment.Delete(id);
        }

        private string GetBindUrl()
        {
            return "currpostfix=" + currpostfix + "&keyword=" + keyword + "&isexact=" + isexact + "&islocked=" + islocked + "&showsql=" + showsql + "&page";
        }

        //批量删除
        protected void ImageButton_all_del_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {

            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    Delete_msgs(id);
                    nub += 1;
                }
            }
            Response.Redirect("/Admin/Message.aspx?message=批量删除<b>" + nub + "</b>条信息成功！&z=yes");
        }

        protected void ImageButton_all_unlock_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    curComment.UpdateLocked(id, false);
                    nub += 1;
                }
            }
            Response.Redirect("/Admin/Message.aspx?message=批量审核<b>" + nub + "</b>条信息成功！&z=yes");
        }


    }
}
